﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "BTTaskNode/TaskAIAttack.h"

#include "AIController.h"
#include "Character/EnemyCharacter.h"
#include "Helper/DebugHelper.h"


UTaskAIAttack::UTaskAIAttack()
{
	NodeName = FString("AIAttack");
	// 开启tickTask
	bNotifyTick = true;
}

EBTNodeResult::Type UTaskAIAttack::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	AAIController* AaiController = OwnerComp.GetAIOwner();
	ABaseCharacter* BaseCharacter = Cast<ABaseCharacter>(AaiController->GetPawn());
	if (AaiController == nullptr || BaseCharacter == nullptr)
	{
		return EBTNodeResult::Failed;
	}
	BaseCharacter->PlayAttackMontage();
	// 等待完成
	return EBTNodeResult::InProgress;
}

uint16 UTaskAIAttack::GetSpecialMemorySize() const
{
	return Super::GetSpecialMemorySize();
}

void UTaskAIAttack::TickTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
	AAIController* AIOwner = OwnerComp.GetAIOwner();
	if (AIOwner != nullptr)
	{
		ABaseCharacter* BaseCharacter = Cast<ABaseCharacter>(AIOwner->GetPawn());
		if (BaseCharacter && !BaseCharacter->IsAttacking_AI)
		{
			//Debug::printLog("WrappedTickTask finished");
			// 真正的完成
			FinishLatentTask(OwnerComp, EBTNodeResult::Succeeded);
		}
	}
}
